<?php

class TasksController extends Zend_Controller_Action
{
    protected $_form;
    protected $_tasks;
    protected $_users;

    public function init()
    {
        $this->_form = new Application_Form_Task();
        $this->_tasks = new Application_Model_Tasks();
        $this->_users = new Application_Model_Users();
    }

	public function preDispatch()
    {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_helper->redirector('index', 'login');
        }
    }

    public function indexAction()
    {
        $this->view->title = 'Задачи';
        $this->view->headTitle($this->view->title);

        $this->view->tasks = $this->_tasks->getTaskList();
        $this->_helper->log("Преглед на списък задачи");
    }

    public function createAction()
    {
        $this->view->title = 'Създаване на задача';
        $this->view->headTitle($this->view->title);

        $action = $this->_helper->url('create', 'tasks');
        $this->_form->setAction($action);
        $this->view->form = $this->_form;

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
			
        	if (strtotime($formData['date_start']) == false) {
				$this->_form->getElement('date_start')->markAsError();
				$this->view->invalid_date_start = 1;
            }
            
            if (strtotime($formData['date_end']) == false) {
				$this->_form->getElement('date_end')->markAsError();
				$this->view->invalid_date_end = 1;
            }
            
       		if ($formData['date_start'] > $formData['date_end']) {
				$this->_form->getElement('date_start')->markAsError();
				$this->view->invalid_date_interval = 1;
            }
            
            if ($this->_form->isValid($formData)) {
                $this->_tasks->createTask($this->_form->getValues());
                $this->_helper->log("Създаване на задача");
                $this->_helper->redirector('index', 'tasks');
            } else {
                $this->_form->populate($formData);
            }
        }
    }

    public function editAction()
    {
        $this->view->title = 'Редактиране на задача';
        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        $action = $this->_helper->url('edit', 'tasks', null, array('id' => $id));
        $this->_form->setAction($action);
        $this->view->form = $this->_form;

        if ($this->getRequest()->isPost()) {

            $formData = $this->getRequest()->getPost();
            
            if (strtotime($formData['date_start']) == false) {
				$this->_form->getElement('date_start')->markAsError();
				$this->view->invalid_date_start = 1;
            }
            
            if (strtotime($formData['date_end']) == false) {
				$this->_form->getElement('date_end')->markAsError();
				$this->view->invalid_date_end = 1;
            }
            
            if ($formData['date_start'] > $formData['date_end']) {
				$this->_form->getElement('date_start')->markAsError();
				$this->view->invalid_date_interval = 1;
            }
            
            if ($this->_form->isValid($formData)) {
                $this->_tasks->editTask($id, $this->_form->getValues());
                $this->_helper->log("Редактиране на задача $id");
                $this->_helper->redirector('index', 'tasks');
            } else {
                $this->_form->populate($formData);
            }

        } else {

            try {
                $this->_form->populate($this->_tasks->getTask($id));
            } catch (Exception $e) {
                $this->_helper->redirector('notfound', 'error');
            }

        }
    }

    public function viewAction()
    {
        $this->view->title = 'Преглед на задача';
        $this->view->headTitle($this->view->title);

        $id = $this->_getParam('id', 0);

        try {
            $task = $this->_tasks->getTask($id);
            $supervisor = $this->_users->getUser($task['supervisor']);
            $task['supervisor_name'] = $supervisor['first_name'] . ' ' . $supervisor['last_name'];
            $task['status_name'] = Application_Model_Statuses::$statuses[$task['status']];

            $users = $this->_tasks->getTaskUsers($id);
            $hours = 0;
            foreach ($users as $user) {
                $hours += intval($user['hours']);
            }
            $task['hours_current'] = $hours;

            $this->view->task = $task;
            $this->view->users = $users;

            $this->_helper->log("Преглед на задача $id");
        } catch (Exception $e) {
            $this->_helper->redirector('notfound', 'error');
        }
    }


}







